import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';


class WebView extends StatefulWidget {

  final arguments;

  WebView({this.arguments});

  @override
  _WebView createState() => _WebView();
}

class _WebView extends State<WebView> {

  final flutterWebviewPlugin = FlutterWebviewPlugin();
  Completer<bool> _finishedcompleter = Completer();

  @override
  void initState() {
    super.initState();
    //监听网页加载的网页.
    flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
      debugPrint('onStateChanged: ${state.type} ${state.url}');
      if (state.type == WebViewState.finishLoad) {
        _finishedcompleter.complete(true);
      }
    });
  }

  @override
  void dispose() {
    //关闭各种网页调用
    flutterWebviewPlugin.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    return WebviewScaffold(
      appBar: AppBar(title: Text(widget.arguments),),
      url: widget.arguments,
      //实例化一个webView，让webView占满剩余的屏幕. 没有设置其他4个属性好像网页只会显示一小块
      withZoom: true,
      //网页是否可以放大
      withLocalStorage: true,
      //是否可以本地储存
      hidden: true,
      // 是否可以隐藏
      initialChild: Container(
        //初始化的时候网页要显示什么。
        color: Colors.white,
        child: Center(
          child: Text('${widget.arguments} waiting...'),
        ),
      ),
    );
  }
}
